package com.party.mobile.redisPubSub.subscribe;

import com.party.mobile.redisPubSub.listener.RedisMsgPubSubListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

/**
 * @Description: 订阅频道
 * @Author: yangshoukun
 * @Date: 2018/4/2 19:00
 */
@Component
public class SubScribeMessage {
    @Autowired
    private JedisConnectionFactory connectionFactory;

    @Autowired
    private RedisMsgPubSubListener redisMsgPubSubListener;

    public static final String TRANSFER_CHANNEL_NAME = "projectTansferMobile";

    public void subscribeChannel(final String channel) {
        new Thread(() -> {
            JedisConnection connection = connectionFactory.getConnection();
            Jedis jedis = connection.getNativeConnection();
            jedis.subscribe(redisMsgPubSubListener, channel);
            connection.close();
        }).start();
    }
}
